load MAT/WIOD.mat france N J start;
SHOCK_scheme = {'WORLDprod_p10', 'WORLDpref_p10'};
specs = 2;
for j = 1:2
      shockT = SHOCK_scheme{j};
 
      table_e_Y_cpi = zeros(N,specs);
      table_e_Y_def = zeros(N,specs);
      table_e_Y_doubledef = zeros(N,specs);
      table_m_e_mj_cpi = zeros(N,specs);
      table_m_e_mj_def = zeros(N,specs);
      table_m_e_mj_doubledef = zeros(N,specs);
      table_cov_e_mj_cpi = zeros(N,specs);
      table_cov_e_mj_def = zeros(N,specs);
      table_cov_e_mj_doubledef = zeros(N,specs);
      table_e_f_cpi = zeros(specs,3);
      table_e_f_def = zeros(specs,3);
      table_e_f_doubledef = zeros(specs,3);
      table_e_fmj_cpi = zeros(specs,3);
      table_e_fmj_def = zeros(specs,3);
      table_e_fmj_doubledef = zeros(specs,3);
      
      
      % Heterogeneous-firm results

     eval([strcat('load MAT/rho',rhoT,'eta',etaT,'lambda',lambdaT,'_psi',num2str(varphi),'_sigma',num2str(sigmaT),'/Step_6_output_alpha',num2str(alphaT),...
         '_rho',rhoT,'_',shockT,'_approx.mat')]); 
          
      table_e_Y_cpi(:,1) = e_Y_s_cpi;
      table_e_Y_def(:,1) = e_Y_s_def;
      table_e_Y_doubledef(:,1) = e_Y_s_doubledef;
      table_m_e_mj_cpi(:,1) = m_e_mj_cpi;
      table_m_e_mj_def(:,1) = m_e_mj_def;
      table_m_e_mj_doubledef(:,1) = m_e_mj_doubledef;
      table_cov_e_mj_cpi(:,1) = cov_e_mj_cpi;
      table_cov_e_mj_def(:,1) = cov_e_mj_def;
      table_cov_e_mj_doubledef(:,1) = cov_e_mj_doubledef;
      
      table_e_f_cpi(1,1) = e_Y_FRf_cpi;
      table_e_f_cpi(1,2) = m_e_f_cpi;
      table_e_f_cpi(1,3) = cov_e_f_cpi;
      table_e_f_def(1,1) = e_Y_FRf_def;
      table_e_f_def(1,2)= m_e_f_def;
      table_e_f_def(1,3) = cov_e_f_def;
      table_e_f_doubledef(1,1) = e_Y_FRf_doubledef;
      table_e_f_doubledef(1,2)= m_e_f_doubledef;
      table_e_f_doubledef(1,3) = cov_e_f_doubledef;
      
      table_e_fmj_cpi(1,1) = e_Y_FRs_cpi;
      table_e_fmj_cpi(1,2) = m_e_mjFRs_cpi;
      table_e_fmj_cpi(1,3) = cov_e_mjFRs_cpi;
      table_e_fmj_def(1,1) = e_Y_FRs_def;
      table_e_fmj_def(1,2) = m_e_mjFRs_def;
      table_e_fmj_def(1,3) = cov_e_mjFRs_def;
      table_e_fmj_doubledef(1,1) = e_Y_FRs_doubledef;
      table_e_fmj_doubledef(1,2) = m_e_mjFRs_doubledef;
      table_e_fmj_doubledef(1,3) = cov_e_mjFRs_doubledef;
      
      
      
%       % Homogeneous-firm results
%       
% 
     eval([strcat('load MAT/rho',rhoT,'eta',etaT,'lambda',lambdaT,'_psi',num2str(varphi),'_sigma',num2str(sigmaT),'/Step_6_output_alpha',num2str(alphaT),...
         '_rho',rhoT,'_',shockT,'_Hom_approx.mat')]); 
%      
       table_e_Y_cpi(:,2) = e_Y_s_cpi;
       table_e_Y_def(:,2) = e_Y_s_def;
       table_e_Y_doubledef(:,2) = e_Y_s_doubledef;
       table_m_e_mj_cpi(:,2) = m_e_mj_cpi;
       table_m_e_mj_def(:,2) = m_e_mj_def;
       table_m_e_mj_doubledef(:,2) = m_e_mj_doubledef;
       table_cov_e_mj_cpi(:,2) = cov_e_mj_cpi;
       table_cov_e_mj_def(:,2) = cov_e_mj_def;
       table_cov_e_mj_doubledef(:,2) = cov_e_mj_doubledef;
% 
       table_e_f_cpi(2,1) = e_Y_FRf_cpi;
       table_e_f_cpi(2,2) = m_e_f_cpi;
       table_e_f_cpi(2,3) = cov_e_f_cpi;
       table_e_f_def(2,1) = e_Y_FRf_def;
       table_e_f_def(2,2)= m_e_f_def;
       table_e_f_def(2,3) = cov_e_f_def;
       table_e_f_doubledef(2,1) = e_Y_FRf_doubledef;
       table_e_f_doubledef(2,2)= m_e_f_doubledef;
       table_e_f_doubledef(2,3) = cov_e_f_doubledef;
%       
       table_e_fmj_cpi(2,1) = e_Y_FRs_cpi;
       table_e_fmj_cpi(2,2) = m_e_mjFRs_cpi;
       table_e_fmj_cpi(2,3) = cov_e_mjFRs_cpi;
       table_e_fmj_def(2,1) = e_Y_FRs_def;
       table_e_fmj_def(2,2) = m_e_mjFRs_def;
       table_e_fmj_def(2,3) = cov_e_mjFRs_def;
       table_e_fmj_doubledef(2,1) = e_Y_FRs_doubledef;
       table_e_fmj_doubledef(2,2) = m_e_mjFRs_doubledef;
       table_e_fmj_doubledef(2,3) = cov_e_mjFRs_doubledef;

       if shockT=='WORLDprod_p10'
          tab3(1:2,1:3) = table_e_f_doubledef;
          tab3(3,1:3)= table_e_fmj_doubledef(1,:);
          tabA4(1:2,1:3) = table_e_f_cpi;
          tabA4(3,1:3)= table_e_fmj_cpi(1,:);
      else if shockT=='WORLDpref_p10'
          tab3(1:2,4:6) = table_e_f_doubledef;
          tab3(3,4:6)= table_e_fmj_doubledef(1,:);
         tabA4(1:2,4:6) = table_e_f_cpi;
          tabA4(3,4:6)= table_e_fmj_cpi(1,:);
          end
       end
      save('Tables/tab3.mat','tab3'); 
      save('Tables/tabA4.mat','tabA4'); 

            %%%%%%%%%%%%%%%%%%%%%%%%%
      % Create tables for Excel
      %%%%%%%%%%%%%%%%%%%%%%%%%

      
      E_Y = table(table_e_Y_cpi(:,1),table_e_Y_cpi(:,2),table_e_Y_def(:,1),table_e_Y_def(:,2),table_e_Y_doubledef(:,1),table_e_Y_doubledef(:,2));
      E_Y.Properties.VariableNames = {'HeteroCPI' 'HomCPI' 'HeteroDEF' 'HomDEF' 'HeteroDDEF' 'HomDDEF'};
      
      M_E_MJ = table(table_m_e_mj_cpi(:,1),table_m_e_mj_cpi(:,2),table_m_e_mj_def(:,1),table_m_e_mj_def(:,2),table_m_e_mj_doubledef(:,1),table_m_e_mj_doubledef(:,2));
      M_E_MJ.Properties.VariableNames = {'HeteroCPI' 'HomCPI' 'HeteroDEF' 'HomDEF' 'HeteroDDEF' 'HomDDEF'};
      
      COV_E_MJ = table(table_cov_e_mj_cpi(:,1),table_cov_e_mj_cpi(:,2),table_cov_e_mj_def(:,1),table_cov_e_mj_def(:,2),table_cov_e_mj_doubledef(:,1),table_cov_e_mj_doubledef(:,2));
      COV_E_MJ.Properties.VariableNames = {'HeteroCPI' 'HomCPI' 'HeteroDEF' 'HomDEF' 'HeteroDDEF' 'HomDDEF'};
      
      E_F = table(table_e_f_cpi(:,1),table_e_f_cpi(:,2),table_e_f_cpi(:,3),table_e_f_def(:,1),table_e_f_def(:,2),table_e_f_def(:,3),table_e_f_doubledef(:,1),table_e_f_doubledef(:,2),table_e_f_doubledef(:,3));
      E_F.Properties.VariableNames = {'e_Y_FR_CPI' 'm_e_f_CPI' 'cov_e_f_CPI' 'e_Y_FR_DEF' 'm_e_f_DEF' 'cov_e_f_DEF'  'e_Y_FR_DDEF' 'm_e_f_DDEF' 'cov_e_f_DDEF'};
      E_F.Properties.RowNames = {'Hetero' 'Hom'};

      E_Fmj = table(table_e_fmj_cpi(:,1),table_e_fmj_cpi(:,2),table_e_fmj_cpi(:,3),table_e_fmj_def(:,1),table_e_fmj_def(:,2),table_e_fmj_def(:,3),table_e_fmj_doubledef(:,1),table_e_fmj_doubledef(:,2),table_e_fmj_doubledef(:,3));
      E_Fmj.Properties.VariableNames = {'e_Y_FRs_CPI' 'm_e_mj_FRs_CPI' 'cov_e_mj_FRs_CPI' 'e_Y_FRs_DEF' 'm_e_mj_FRs_DEF' 'cov_e_mj_FRs_DEF' 'e_Y_FRs_DDEF' 'm_e_mj_FRs_DDEF' 'cov_e_mj_FRs_DDEF'};
      E_Fmj.Properties.RowNames = {'Hetero' 'Hom'};
      
      %%%%%%%%%%%%%%%%%%%%%%%
      % Write tables to Excel
      %%%%%%%%%%%%%%%%%%%%%%%     
   
      filename=['Tables/Tables_rho' rhoT 'eta' etaT 'lambda' lambdaT  '_psi' num2str(varphi) '_sigma' sigmaT '_alpha' num2str(alphaT) '_rho' rhoT '_' shockT '.xlsx'];
      
      writetable(E_Y,filename,'Sheet','Elasticities_GDP');
      writetable(M_E_MJ,filename,'Sheet','Elasticities_MeanSec');
      writetable(COV_E_MJ,filename,'Sheet','Elasticities_CovSec');
      writetable(E_F,filename,'Sheet','Elasticities_FirmDecomp','WriteRowNames',true);
      writetable(E_Fmj,filename,'Sheet','Elasticities_SectorDecompFR','WriteRowNames',true);
end

rownames = {'Baseline','Homogeneous firms','Sectoral'};
ProdTot = tab3(:,1);
ProdMean = tab3(:,2);
ProdCov = tab3(:,3);
PrefTot = tab3(:,4);
PrefMean = tab3(:,5);
PrefCov = tab3(:,6);
Tab3 = table(ProdTot,ProdMean,ProdCov,PrefTot,PrefMean,PrefCov,'Rownames',rownames);%,'VariableNames',colnames);
writetable(Tab3,'Tables/Tab3.xlsx','Sheet','Tab3','WriteRowNames',true);

ProdTot = tabA4(:,1);
ProdMean = tabA4(:,2);
ProdCov = tabA4(:,3);
PrefTot = tabA4(:,4);
PrefMean = tabA4(:,5);
PrefCov = tabA4(:,6);
TabA4 = table(ProdTot,ProdMean,ProdCov,PrefTot,PrefMean,PrefCov,'Rownames',rownames);%,'VariableNames',colnames);
writetable(TabA4,'Tables/TabA4.xlsx','Sheet','TabA4','WriteRowNames',true);

